// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MisinkoMaster

//@version=6
indicator("Market Extreme Zones Index", "MEZI | MisinkoMaster", overlay = false)
//////////////////////////////////////////////////////////////////////////////////
//Import Libraries
import TradingView/ta/11
//////////////////////////////////////////////////////////////////////////////////
//Get User Defined Inputs
src = input.source(close, title = "Source:",
     tooltip = "Changes the source used for the calculations",
     group = "Market Extreme Zone Index | MisinkoMaster")
matype = input.string("EMA", title = "Moving Average:",
     options = ["SMA", "RMA", "EMA", "WMA", "REMA", "DEMA", "TEMA", "HMA"],
     tooltip = "Changes the Moving Average Used. Recommended to only use EMA.", 
     group = "Market Extreme Zone Index | MisinkoMaster")
len = input.int(156, title = "Length",
     tooltip = "Changes the length for the base calculations",
     group = "Market Extreme Zone Index | MisinkoMaster", step = 1, minval = 2)
zlen = input.int(108, title = "Z-Score Length",
     tooltip = "Changes the length of the Z-Score",
     group = "Market Extreme Zone Index | MisinkoMaster", step = 1, minval = 2)
//////////////////////////////////////////////////////////////////////////////////
//Calculations
F_MEZI(source, moving_average_type, length, z_score_length) =>
    //Get Lengths
    half_length = math.abs(math.round(length/2)) //Half Length
    sqrt_length = math.abs(math.round(math.sqrt(length))) //Square root of the length
    //RSI Calculations
    rsi1 = ta.rsi(source, length) //First RSI with basic length and source
    rsi2 = ta.rsi(source, half_length) //Second RSI with half of the length and normal source
    rsi3 = ta.rsi(rsi2, half_length) //3rd RSI with half of the basic length on the 2nd RSI
    rsi = rsi2*3-rsi1-rsi3 //RSI Divergence making it less lagging
    med = ta.median(rsi, sqrt_length) //Median RSI over square root of the length
    RSI = rsi*2 - med //RSI modified by doubling it and subtracting the median
    //Z-Score Calculations
    avg = switch matype //Average RSI value
        "SMA" => ta.sma(RSI, z_score_length) //SMA
        "RMA" => ta.rma(RSI, z_score_length) //RMA
        "EMA" => ta.ema(RSI, z_score_length) //EMA
        "WMA" => ta.wma(RSI, z_score_length) //WMA
        "REMA" => ta.ema(RSI, z_score_length)*(1-(2/(1+z_score_length))) //More Reactive EMA
             +source*(2/(1+z_score_length)) 
        "DEMA" => ta.dema(RSI, z_score_length) //DEMA
        "TEMA" => ta.tema(RSI, z_score_length) //TEMA
        "HMA" => ta.hma(RSI, z_score_length) //HMA
    //Z-Score Calculations
    sd = ta.stdev(RSI, z_score_length, true) //Standard Deviation Calculation
    base_MEZI = (RSI - avg) / sd //Z-Score
    //Smoothing
    MEZI = ta.wma(base_MEZI, sqrt_length) //Smoothed Result

MEZI = F_MEZI(src, matype, len, zlen)
//////////////////////////////////////////////////////////////////////////////////
//Plotting & Conditions
//Conditions
col = color.rgb(71, 69, 69)
if MEZI < -2
    col := color.rgb(20, 240, 13)
if MEZI < -1 and MEZI > -2
    col := color.rgb(143, 245, 47)
if MEZI > -1 and MEZI < 0
    col := color.rgb(251, 255, 0)
if MEZI < 1 and MEZI > 0
    col := color.rgb(236, 159, 16)
if MEZI > 1 and MEZI < 2
    col := color.rgb(253, 83, 83)
if MEZI > 2
    col := color.rgb(238, 13, 21)
//Transparent Colors (Between Treshholds)
colYT = color.rgb(251, 255, 0, 70)
colGT = color.rgb(143, 245, 4, 70)
colRT = color.rgb(253, 83, 83, 70)
colOT = color.rgb(236, 159, 16, 70)
//Trasparent Colors (For highlighting extreme zones)
colRRT = color.rgb(238, 13, 21, 70)
colGGT = color.rgb(20, 240, 13, 70)
//Plotting Indicator Values
plot(MEZI, title = "Market Extreme Zone Index | MisinkoMaster", color = col, linewidth = 3)
//Plotting Treshholds
a = plot(2, color = color.rgb(238, 13, 21), display = display.pane)
b = plot(1, color = color.rgb(253, 83, 83), display = display.pane)
c = plot(0, color = color.rgb(251, 255, 0), display = display.pane)
d = plot(-1, color = color.rgb(143, 245, 47), display = display.pane)
e = plot(-2, color = color.rgb(20, 240, 13), display = display.pane)
//Filling spaces between treshholds
fill(b, c, colOT)
fill(c, d, colYT)
fill(a, b, colRT)
fill(d, e, colGT)
//Background plotting for extreme conditions
bgcolor(MEZI > 2 ? colRRT : na, force_overlay = true)
bgcolor(MEZI > 2 ? colRRT : na, force_overlay = false)
bgcolor(MEZI < -2 ? colGGT : na, force_overlay = true)
bgcolor(MEZI < -2 ? colGGT : na, force_overlay = false)
//Candle coloring for better recognition
plotcandle(open, high, low, close, bordercolor = col, color = col, force_overlay = true, display = display.pane)